//
//  DealTool.m
//  小团购
//
//  Created by 樊荣海 on 15/1/30.
//  Copyright (c) 2015年 樊荣海. All rights reserved.
//

#import "DealTool.h"



@implementation DealTool {
    
    NSMutableDictionary *_blocksDict;//所有block 字典
}

singleton_implementation(DealTool)

- (instancetype)init {
    
    if (self == [super init]) {
        
        _blocksDict = [NSMutableDictionary dictionary];
    }
    
    return self;
}

- (void)dealToolWithDeal_ID:(NSString *)ID success:(DealSuccessBlock)successBlock fail:(DealFailBlock)failBlock {
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];

    [params setObject:ID forKey:@"deal_id"];

    [self requestWithURL:@"v1/deal/get_single_deal" params:params block:^(id result, NSError *aError) {
        
        if (aError) {
            
            if (failBlock) {
                failBlock(aError);
            }
            
        } else if (successBlock) {

            DealModel *deal = [[DealModel alloc] initWithDictionary:result[@"deals"][0]];

            successBlock(deal);
            
        }
        
    }];


}

- (void)dealToolWithPage:(int)page success:(DealsSuccessBlock)successBlock fail:(DealsFailBlock)failBlock {

    //1.城市
    NSString *city = [[DBTool sharedDBTool] selectCurrentCity];
    
    //2.区域
    NSString *region = [[DBTool sharedDBTool] selectSubTitle:TabelTypeDis];
    
    //3.分类
    NSString *cate = [[DBTool sharedDBTool] selectSubTitle:TabelTypeCate];
    
    //4.排序
    NSArray *array = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Orders" ofType:@"plist"]];
    NSInteger index = [array indexOfObject:[[DBTool sharedDBTool] selectSubTitle:TabelTypeOrder]]+1;
    
    if (index < 0) {index =1;}
    
    // 1.封装请求参数
    NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObject:@(15) forKey:@"limit"];
    
    if (city.length>0) {
        [params setObject:city forKey:@"city"];
    } else {
        [params setObject:@"上海" forKey:@"city"];
    }
    
    if (region.length>0 && ![region isEqualToString:@"全部商区"]) {
        [params setObject:region forKey:@"region"];
    }
    
    if (cate.length>0 && ![cate isEqualToString:@"全部分类"]) {
        [params setObject:cate forKey:@"category"];
    }
    
    if (index>0) {
        [params setObject:@(index) forKey:@"sort"];
    }
    
    //添加页码参数
    [params setObject:@(page) forKey:@"page"];

    NSLog(@"正在请求, 请求页数: %d", page);
//    NSLog(@"请求参数:%@", params);
    
    [self requestWithURL:@"v1/deal/find_deals" params:params block:^(id result, NSError *aError) {
        
        if (aError) {
            
            if (failBlock) {
                failBlock(aError);
            }
            
        } else if (successBlock) {
            
            NSArray *deals = result[@"deals"];

            NSMutableArray *arrayTemp = [NSMutableArray array];
            
            for (NSDictionary *dict in deals) {
                DealModel *d = [[DealModel alloc] initWithDictionary:dict];
            
                [arrayTemp addObject:d];
            }
            
            successBlock(arrayTemp);

        }
  
    }];
    
}

- (void)requestWithURL:(NSString *)url params:(NSDictionary *)params block:(RequestBlock)requestBlock {
    
    // http://api.dianping.com/v1/deal/find_deals?city=上海&region=黄浦区&category=电影&limit=1&page=1&appkey=[appkey]&sign=[signature]
    
  
    
    DPRequest *request = [[DPAPI sharedDPAPI] requestWithURL:url
                                                      params:params
                                                    delegate:self];
    
    NSLog(@"请求参数:%@", params);
    
    //一次请求 对应一个block
    [_blocksDict setObject:requestBlock forKey:request.description];
    
}


- (void)request:(DPRequest *)request didFinishLoadingWithResult:(id)result {
    
    RequestBlock requestBlock = _blocksDict[request.description];
    
    if (requestBlock) {
        requestBlock(result, nil);
    }
    
}


- (void)request:(DPRequest *)request didFailWithError:(NSError *)error {
    
    RequestBlock requestBlock = _blocksDict[request.description];
    
    if (requestBlock) {
        requestBlock(nil, error);
    }
    
}















@end
